{
    let Utils = Sail.Utils;

    Utils.cookieStore = {
        get: function (name) {
            let cookieName = encodeURIComponent(name) + "=",
                cookieStart = document.cookie.indexOf(cookieName),
                cookieValue = null;

            if (cookieStart > -1) {
                let cookieEnd = document.cookie.indexOf(";", cookieStart)
                if (cookieEnd == -1) {
                    cookieEnd = document.cookie.length;
                }
                cookieValue = decodeURIComponent(document.cookie.substring(cookieStart + cookieName.length, cookieEnd));
            }

            return cookieValue;
        },
        set: function (name, value, expires, path, domain, secure) {
            let cookieText = encodeURIComponent(name) + "=" + encodeURIComponent(value);

            if (expires instanceof Date) {
                cookieText += "; expires=" + expires.toGMTString();
            }

            if (path) {
                cookieText += "; path=" + path;
            }

            if (domain) {
                cookieText += "; domain=" + domain;
            }

            if (secure) {
                cookieText += "; secure";
            }

            document.cookie = cookieText;
        },
        unset: function (name, path, domain, secure) {
            this.set(name, "", new Date(0), path, domain, secure);
        }
    }

    //获取随机头像
    Utils.getAvatar = function(userId){
        let self = userId == GM.user_id;
        let coockieName = 'avatar';
        let avatarId = Utils.cookieStore.get(coockieName+userId);
        let expire = null;
        let date = null;
        if(!avatarId){
            avatarId = parseInt(Math.random()*14)+1;
        }
        if(self){
            date = new Date();
            expire = new Date(date.setDate(date.getDate()+2));
        }
        Utils.cookieStore.set(coockieName+userId, avatarId, expire);
        return `res/avatar/avtar${avatarId}.png`;
    }

    /**
     * @public
     * 数值转化为万单位
     * @param {String} num 原数值
     * @param {boolean} flag 是否舍弃小数位
     */
    Utils.transferNumberToK = function(num, flag){
        if(num==0){return "0";}
        if(!num || num === ""){return "";}
        num = parseInt(num);
      	if(num < 10000){
		  return num;
		}else if(num >= 10000 &&　num < 100000000){
			let strNum = flag ? String(Math.floor(num/10000)) :String(num/10000);
			num = Number(strNum.slice(0,5)) + "万";
			return num;
		}else if(num >= 100000000){
			let strNum = String(num/100000000);
			num = Number(strNum.slice(0,5)) + "亿";
			return num;
		}
    }
    Utils.modifyNumber = function(num){
        if(isNaN(num)){
            return;
        }
        if(num >= 10000){
            num = num/10000 + '万';
        }else if(num >= 1000){
            num = num/1000 + '千';
        }
        return num;
    }
    Utils.formatMoney = function (number, decimal, isround){
        let _number = number + "";
        //判断是否是负数
        let smallthanzero = _number.indexOf("-") == -1 ? false : true;
        //去除负号
        let na = smallthanzero ? _number.replace(/-/ig,"").split(".") : _number.split(".");
        let result = [];
        
        //处理整数部分
        let n = 0;
        for(let i = na[0].length - 1; i >= 0; i--){
            if(n % 3 == 0 && n != 0){
                result.push(",");
            }
            result.push(na[0][i]);
            n++;
        }
        
        //反转数组
        result.reverse();
        
        //判断是否要加回负号
        let s = smallthanzero ? "-" : "";

        //小数部分处理
        let _d = na[1];
        if(_d && decimal){
            _d = na[1].substring(0,decimal);
        }
        
        return _d ? s + result.join("") + "." + _d : s + result.join("");
    }
    /**
     * 获取m到n的随机数，包含m,n
     */
    Utils.getRandom = function (m, n) {
        return Math.floor(Math.random() * (m - n) + n);
    }
    Utils.checkLogin = function () {
        if(!USER_LOGIN_STATUS){
            location.href = GM.userLoginUrl;
            return false;
        }else{
            return true;
        }
    }
    Utils.recharge = function () {
        if(Utils.checkLogin()){
            if(GM.appStorePay == 1 && (window.wltgame && wltgame.suportStorePay == 1) || GM.appStorePay == 2){
                location.href = "/?act=ios_applepay&st=applepay_game_vertical&gameId=" + GM.gameId + "&redirect_uri=" + redirect_uri;
            }else{
                Sail.director.popScene(new Alert.Recharge);
            }
        }
    }


    // 把秒转化为时分秒
    Utils.toDetailTime = (num) => {
        var time = Number(num);
        var days = Math.floor(time / 1440 / 60);
        var hours = Math.floor((time - days * 1440 * 60) / 3600);
        var minutes = Math.floor((time - days * 1440 * 60 - hours * 3600) / 60);
        var seconds = (time - days * 1440 * 60 - hours * 3600 - minutes * 60);
        var result = '';
        if (days) result += days + 'd';
        if (hours) result += hours + 'h';
        if (minutes) result += minutes + 'm';
        // if (seconds) result += seconds + 's';
        if(result == ''){
             result = 0;
        }
        
        return result;
    }

    // 倒计时
    Utils.createTimeCountDown = function() {
        return {
            // 创建 (总时间, 倒计时进行回调， 倒计时结束回调)
            start(time, timeGoingCb, timeOutCb) {
                let count = Number(time);

                this.loop = () => {
                    if (--count > 0) {
                        timeGoingCb && timeGoingCb(count);
                        Laya.timer.once(1000, this, this.loop);

                    } else {
                        timeOutCb && timeOutCb();
                        this.clear();
                    }
                }

                this.loop();
            },
            clear() {
                if(this.loop){
                    Laya.timer.clear(this, this.loop);
                    this.loop = null;
                }

            }
        }
    }
}